package com.heqiang.web.servlet;


import com.heqiang.domain.PageBean;
import com.heqiang.service.UserService;
import com.heqiang.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/findUserByPageServlet")
public class FindUserByPageServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String currentPage = request.getParameter("currentPage");//当前页码
        String rows = request.getParameter("rows");//每页显示条数

        if (currentPage == null || currentPage.equals("")||Integer.parseInt(currentPage)<1) {
            currentPage = "1";
        }
        if (rows == null || rows.equals("")) {
            rows = "5";
        }

        //接收三个参数
        String name = request.getParameter("name");
        String address = request.getParameter("address");
        String email = request.getParameter("email");

        if (name == null) {
            name = "";
        }
        if (address == null) {
            address = "";
        }
        if (email == null) {
            email = "";
        }

        //创建对象,调用方法,返回Pagebeen对象
        UserService service = new UserServiceImpl();
        PageBean pb = service.findUserByPage(currentPage, rows, name, address, email);
        //存储数据,转发页面
        request.setAttribute("pb", pb);
        request.setAttribute("name", name);
        request.setAttribute("address", address);
        request.setAttribute("email", email);

        request.getRequestDispatcher("/list.jsp").forward(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
